package com.hyjy.music.widget

import android.content.Context
import android.graphics.*
import androidx.appcompat.widget.AppCompatTextView
import com.hyjy.music.R
import android.util.AttributeSet

/**
 * 精简的歌词View,显示一行歌词。
 */
class TidyLrcView : AppCompatTextView {
    private val lrcNotExist = "暂无歌词"
    private var lrcContent = ""
    private var lrcPercent = 0F
    private var lrcSize = 0F // 歌词字体大小
    private lateinit var defaultPaint: Paint // 默认画笔
    private lateinit var highlightPaint: Paint // 高亮歌词画笔
    private var textBounds: Rect = Rect()
    private var clipRectF: RectF = RectF()
    private var width1 = 0
    private var height1 = 0

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    /**
     * 初始化
     */
    private fun init() {
        lrcSize = resources.getDimension(R.dimen.size_highlight)
        defaultPaint = Paint().apply {
            color = Color.WHITE
            textSize = lrcSize
            isAntiAlias = true // 抗锯齿
            textAlign = Paint.Align.LEFT // 设置文本对齐方式
        }

        highlightPaint = Paint().apply {
            color = Color.RED
            textSize = lrcSize
            isAntiAlias = true
            textAlign = Paint.Align.LEFT
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldWidth: Int, oldHeight: Int) {
        super.onSizeChanged(w, h, oldWidth, oldHeight)
        width1 = w
        height1 = h
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (lrcContent.isEmpty()) {
            highlightPaint.getTextBounds(lrcNotExist, 0, lrcNotExist.length, textBounds)
            val y = height1 * 0.5f + textBounds.height() * 0.5f
            val textWidth = highlightPaint.measureText(lrcNotExist)
            val x = 0.5F * (width1 - textWidth)
            canvas.drawText(lrcNotExist, x, y, highlightPaint)
            return
        }
        // 先绘制高亮行歌词，得到高亮行歌词的y坐标
        // 高亮行歌词包含两部分,高亮部分和未高亮部分,实现歌词渐进效果
        defaultPaint.getTextBounds(lrcContent, 0, lrcContent.length, textBounds)
        val textHeight = textBounds.height() // 好像不是很精确
        val y = 0.5F * height1 + 0.5F * textHeight
        val textWidth = defaultPaint.measureText(lrcContent)
        val x = 0.5F * (width1 - textWidth)
        // 开始画未高亮部分
        canvas.drawText(lrcContent, x, y, defaultPaint)

        // 开始画高亮部分
        canvas.save()
        // clipRectF.set(x, y - lineHeight, x + textWidth * singedPercent, y),这样设置text底部有一点点没有高亮
        clipRectF.set(x, y - textHeight, x + textWidth * lrcPercent, y + textHeight)
        canvas.clipRect(clipRectF)
        canvas.drawText(lrcContent, x, y, highlightPaint)
        canvas.restore()
    }

    /**
     * 设置歌词进度
     */
    fun setLrcPercent(percent: Float) {
        lrcPercent = percent
        postInvalidate()
    }

    /**
     * 设置歌词文本
     */
    fun setLrcContent(content: String) {
        lrcContent = content
        postInvalidate()
    }

    /**
     * 设置歌词颜色
     */
    fun setLrcColor(color: Int) {
        highlightPaint.color = color
        postInvalidate()
    }
}